package com.woniuxy.carlife.carlifenewcomplaintsweb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.woniuxy.carlife.carlifecommonsweb.util.CopyUtil;
import com.woniuxy.carlife.carlifenewcomplaintsdomain.dto.UserMessageDto;
import com.woniuxy.carlife.carlifenewcomplaintsdomain.param.UserMessageParam;
import com.woniuxy.carlife.carlifenewcomplaintsweb.domain.UserMessage;
import com.woniuxy.carlife.carlifenewcomplaintsweb.mapper.UserMessageMapper;
import com.woniuxy.carlife.carlifenewcomplaintsweb.service.UserMessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author yp
 * @ClassName:UserMessageServiceImpl
 * @Description:用户消息服务
 * @date 2021/4/7 2:32
 **/
@Service
public class UserMessageServiceImpl extends ServiceImpl<UserMessageMapper, UserMessage> implements UserMessageService {

    @Resource
    private UserMessageMapper userMessageMapper;
    /*
     * 功能描述〈添加用户消息〉
     * @author:yp
     * @param:UserMessageParam
     * @return:
     * @Date 2021/4/12 9:58
     * @throws
     */
    @Override
    public int addUserMessage(UserMessageParam userMessageParam) {
        UserMessage userMessage= CopyUtil.getOne(userMessageParam,UserMessage.class);
        int num = userMessageMapper.insert(userMessage);
        return num;
    }

    /*
     * 功能描述〈查询用户消息已读和未读〉
     * @author:yp
     * @param:UserMessageDto
     * @return:UserMessageParam
     * @Date 2021/4/12 10:22
     * @throws
     */
    @Override
    public List<UserMessageDto> findAllUserMessage(Integer tokenUserId) {
        QueryWrapper<UserMessage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",tokenUserId);
        queryWrapper.eq("user_message_state",1);
        queryWrapper.or();
        queryWrapper.eq("user_message_state",2);
        List<UserMessage> list = userMessageMapper.selectList(queryWrapper);
        List<UserMessageDto> userMessageDtoList=CopyUtil.getList(list,UserMessageDto.class);
        return userMessageDtoList;
    }

    /*
     * 功能描述〈修改消息状态〉
     * @author:yp
     * @param: UserMessageParam
     * @return:
     * @Date 2021/4/12 10:34
     * @throws
     */
    @Override
    public int updateUserMessage(Integer userMessageId) {
        UpdateWrapper<UserMessage> wrapper = new UpdateWrapper<>();
        wrapper.eq("user_message_id",userMessageId);
        UserMessage userMessage = new UserMessage();
        userMessage.setUserMessageState(2);
        int num = userMessageMapper.update(userMessage,wrapper);
        return num;
    }

    /*
     * 功能描述〈删除消息〉
     * @author:yp
     * @param:UserMessageParam
     * @return:
     * @Date 2021/4/12 10:39
     * @throws
     */
    @Override
    public int deleteUserMessage(Integer userMessageId) {
        UpdateWrapper<UserMessage> wrapper = new UpdateWrapper<>();
        wrapper.eq("user_message_id",userMessageId);
        UserMessage userMessage = new UserMessage();
        userMessage.setUserMessageState(3);
        int num = userMessageMapper.update(userMessage,wrapper);
        return num;
    }
}
